package com.jingyong.infosite.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.type.TypeReference;
import com.jingyong.infosite.dto.request.NewsAssortListRequest;
import com.jingyong.infosite.dto.request.NewsDetailRequest;
import com.jingyong.infosite.dto.response.NewsAssortListResponse;
import com.jingyong.infosite.dto.response.NewsDetail;
import com.jingyong.infosite.dto.response.NewsImportantResponse;
import com.jingyong.infosite.entities.资讯;
import com.jingyong.infosite.service.InfosService;
import com.jingyong.infosite.util.JsonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path = "news")
@Api("资讯")
public class InfosController {
	@Autowired
	private InfosService service;

	private static final String INFOS_JSON = "[{\"type\":\"要闻\",\"title\":\"网贷日报：P2P收益率又要降？平台退出股东得担责\",\"summary\":\"摘要：2018，P2P走出合规年，进入备案年。当平台越来越少，投资人开始担心一件事：行业竞争不再激烈，网贷利息会不会随之下降？近日，福建省金融工作办公室下发《福建省网络借贷信息中介机构备案登记管理实施细则（试行）（公开征求意见稿）》，指出，P2P实际控制人及持股5%以上的股东应承诺，在平台经营出现困难的时候，要主动配合地方政府有关部门做好风险化解和处置等相关工作。\",\"content\":\"【编者按】作为与您投资息息相关的第三方资讯平台，网贷之家将通过《网贷日报》每天为您梳理P2P网贷江湖大事件，探寻行业最新动向走势，甄别秘诀，拿捏脉点。【今日聚焦】福建P2P备案细则意见稿下发 平台退出股东需担责近日，福建省金融工作办公室下发《福建省网络借贷信息中介机构备案登记管理实施细则（试行）（公开征求意见稿）》，征求意见稿包括五章三十二条。值得注意的是，征求意见稿指出，P2P实际控制人及持股5%以上的股东应承诺，在平台经营出现困难的时候，要主动配合地方政府有关部门做好风险化解和处置等相关工作。\"}]";

	@ApiOperation(value = "查询资讯")
	@RequestMapping(path = "", method = RequestMethod.GET)
	public Object getDeals() {
		return service.query();
	}

//	@ApiOperation(value = "资讯样例", notes = "q={\"type\":\"要闻\"}")
//	@RequestMapping(path = "sample", method = RequestMethod.GET)
	public List<资讯> sample(@RequestParam String q) {
//		资讯选项 opt = JsonUtil.convertStringToBean(q, 资讯选项.class);
		return JsonUtil.convertStringToBean(INFOS_JSON, new TypeReference<List<资讯>>() {
		});
	}

//	@RequestMapping(path = "test", method = RequestMethod.GET)
	public Object test(@RequestParam String years) { // "201711"
		return service.queryDeals(years);
	}

	/**
	 * 获取某条资讯的详细内容
	 * @param req req
	 * @return String
	 */
	@RequestMapping(path = "detail", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public NewsDetail getNewsDetail(@RequestBody NewsDetailRequest req) {
		return service.getNewsDetail(req);
	}

	/**
	 * 获取咨询首页要闻部分
	 * @return String
	 */
	@RequestMapping(path = "important", method = RequestMethod.POST, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	public NewsImportantResponse getNewsImportant() {
		return service.getNewsImportant();
	}

	/**
	 * 获取咨询首页分类新闻列表
	 * @param req req
	 * @return String
	 */
	@RequestMapping(path = "assort/list", method = RequestMethod.POST, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	public NewsAssortListResponse getNewsAssortList(@RequestBody NewsAssortListRequest req) {
		return service.getNewsAssortList(req);
	}

	/**
	 * 获取咨询首页分类新闻列表不带正文的
	 * @param req req
	 * @return String
	 */
	@RequestMapping(path = "assort/list/no/content", method = RequestMethod.POST, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	public NewsAssortListResponse getNewsAssortListNoContent(@RequestBody NewsAssortListRequest req) {
		return service.getNewsAssortListNoContent(req);
	}
}
